package com.mvp.shq.washer.ui.shareApp;

import com.mvp.shq.washer.entity.result.BaseResult;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.utils.SpUtil;
import com.orhanobut.logger.Logger;

/**
 * Created by Administrator on 2017/8/24.
 */

public class ShareAppPresenter extends ShareAppConstract.Presenter {
    @Override
    public void onStart() {
        shareCount();
    }

    @Override
    void shareApp(String type) {
        String token = SpUtil.getToken();
        String userId = SpUtil.getUser().getId();
        String mobile = SpUtil.getUser().getMobile();
        model.shareRecord(userId,mobile,type)
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (t.isError()){
                            view.shareF();
                            Logger.e("分享失败:"+t.getMsg());
                        }else {
                            view.shareS();
                            Logger.d("分享成功");
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        e.printStackTrace();
                        view.shareF();
                    }
                });
    }

    @Override
    void shareCount() {
        String token = SpUtil.getToken();
        String userId = SpUtil.getUser().getId();
        model.shareCount(token,userId)
                .subscribe(new BaseObserver<Integer>(view) {
                    @Override
                    protected void onSuccees(BaseResult<Integer> t) throws Exception {
                        if (t.isError()){
                            view.countF();
                        }else {
                            view.countS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        e.printStackTrace();
                        view.countF();
                    }
                });
    }
}
